package com.example.distributelock.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
public class DemoController {

    public static final Logger log = LoggerFactory.getLogger(DemoController.class);

    private final Lock lock = new ReentrantLock();

    /**
     * 单体锁：存在局限性，服务部署多个，锁失效
     * @return
     */
    @RequestMapping("singleLock")
    public String singleLock() {
        log.info("我进入了方法！");
        lock.lock();
        log.info("我进入了锁！");
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.unlock();
        return "我已经执行完成！";
    }

}
